package com.iot.data;

import com.iot.device.domain.GasClient;
import com.iot.device.domain.QuakeClient;
import com.iot.device.service.IGasClientService;
import com.iot.device.service.IQuakeClientService;
import com.iot.mqtt.callback.GasBeijingMqttCallback;
import com.iot.mqtt.callback.GasClientMqttCallback;
import com.iot.mqtt.callback.PressureSensorClientMqttCallback;
import com.iot.mqtt.callback.QuakeClientMqttCallback;
import com.iot.mqtt.test.HainayunClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;

/**
 * 北斗项目打包的时候，把这个注释掉，注释掉Component就行，保证springboot加载不到这个类
 */
//@Component
public class CommandLine implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(CommandLine.class);

    @Autowired
    public ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    public IQuakeClientService quakeClientService;
    @Autowired
    public IGasClientService gasClientService;
    @Autowired
    public QuakeClientMqttCallback quakeClientMqttCallback;
    @Autowired
    public GasClientMqttCallback gasClientMqttCallback;
    @Autowired
    public GasBeijingMqttCallback gasBeijingMqttCallback;
    @Autowired
    public PressureSensorClientMqttCallback pressureSensorClientMqttCallback;

    @Override
    public void run(String... args) throws Exception {
        // 这个方法里的代码与北斗项目全部无关，里面的内容在打包北斗项目时候不需要
        logger.info("===========开始初始化设备数据");
        List<QuakeClient> quakeClients = quakeClientService.selectQuakeClientList(new QuakeClient());
        List<GasClient> gasClients = gasClientService.selectGasClientList(new GasClient());
        ClientData.setOfflineQuakeClientSet(quakeClients.stream().map(item -> item.getClientNo()).collect(Collectors.toSet()));
        ClientData.setOfflineGasClientSet(gasClients.stream().map(item -> item.getClientNo()).collect(Collectors.toSet()));
        logger.info("===========初始化设备数据完成");
        logger.info("===========开始初始化mqtt服务连接");
        quakeClientMqttCallback.init();
        gasClientMqttCallback.init();
        gasBeijingMqttCallback.init();
        pressureSensorClientMqttCallback.init();
        logger.info("===========mqtt服务连接初始化完成");

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                logger.info("===========海纳云测试mqtt客户端连接初始化");
                    String[] clientNos = new String[]{"KS002537","KS002618","KS002560","KS002605","KS002541","KS002546","KS002562","KS002581","KS002610","KS002612","KS002635","KS002630","KS002637","KS002566","KS002558","KS002593"};
                if(System.getProperty("os.name").indexOf("Windows")>-1) {
                    clientNos = new String[]{};
                }
                for(String clientNo : clientNos){
                    if(!ClientData.quakeClientIsOnline(clientNo)){
                        // 只添加不在线的
                        HainayunClient.initClientConnect(clientNo);
                    }
                }
                logger.info("===========海纳云测试mqtt客户端连接初始化完成");
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 1000 * 60 * 3);
    }
}
